package com.demo06.bubble;

/**
 * @Author: root
 * @Date: 2018/11/14 2:21
 */
public class BubbleSort {
    public static void main(String[] args) {
       int[] arr = {10, 20, 7, 15, 0, 19, 1, 17, 30, 18, -1, 21};
        buSort(arr, arr.length);
        for (int a : arr) {
            System.out.print(a + " ");
        }
        System.out.println();
    }

    private static void buSort(int[] arr, int length) {
        //是否发生改变
        boolean exchange = true;
        //改变的位置
        int postion = length;
        while (exchange) {
            exchange = false;
            int k = postion;
            for (int i = 1; i < k; i++) {
                if (arr[i - 1] >= arr[i]) {
                    int temp = arr[i];
                    arr[i] = arr[i - 1];
                    arr[i - 1] = temp;
                    exchange = true;
                    postion = i;
                }
            }
        }

    }
}
